package com.iotproject.blogservice.controller.admin;

import com.iotproject.blogservice.entity.BlogType;
import com.iotproject.blogservice.service.BlogTypeService;
import com.iotproject.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("admin/blogservice/blogType/")
public class AdminBlogTypeController {

    @Autowired
    BlogTypeService blogTypeService;

    @GetMapping("getArticleTypeList")
    public R getArticleTypeList() {
        QueryWrapper<BlogType> blogTypeQueryWrapper = new QueryWrapper<>();
        blogTypeQueryWrapper.orderByAsc("sort");
        List<BlogType> list = blogTypeService.list(blogTypeQueryWrapper);
        return R.ok().data("blogTypeList", list);
    }

    @DeleteMapping("deleteArticleTypeById/{id}")
    public R deleteArticleTypeById(@PathVariable("id") String id) {
        boolean removeById = blogTypeService.removeById(id);
        if (removeById) {
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("getArticleTypeById/{id}")
    public R getArticleTypeById(@PathVariable("id") String id) {
        BlogType articleType = blogTypeService.getById(id);
        return R.ok().data("articleType", articleType);
    }

    @PostMapping("addArticleType")
    public R addArticleType(@RequestBody BlogType blogType) {
        boolean save = blogTypeService.save(blogType);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    @PostMapping("updateArticleTypeMsg")
    public R updateArticleTypeMsg(@RequestBody BlogType blogType) {
        boolean updateById = blogTypeService.updateById(blogType);
        if (updateById) {
            return R.ok();
        }
        return R.error();
    }

    //传入Excel文件，将内容存储到数据库
    @PostMapping("addArticleTypeByExcel")
    public R upload(MultipartFile file) {
        blogTypeService.importBlogTypeData(file, blogTypeService);
        return R.ok();
    }

}
